package project;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Input implements InputIF {

	Command command;
	String required;
	List<String> optional;
	boolean valid;
	
	Input() {
		
		Scanner in = new Scanner(System.in);
		String[] input = in.nextLine().split("\\s+");
		command = Command.getCommand(input[0]);
		
		optional = new ArrayList<String>();
		
		valid = false;
		if (command != null) {
			for (int a = 1; a < input.length; ++a) {
				if (command.isRequired(input[a])) {
					required = input[a];
					valid = true;
				}
			}
			if (required == null && command.noArgs()) {
				valid = true;
			}
			for (int a = 1; a < input.length; ++a) {
				if (!command.isRequired(input[a])) {
					if (!command.isOptional(input[a])) {
						valid = false;
					} else {
						optional.add(input[a]);
					}
				}
			}
			if (!valid) {
				System.out.println("Usage: "+command.getUsage());
			}
		} else {
			if (Pattern.matches("[a-i][1-9](h|v)?", input[0])) {
				command = Command.MOVE;
				valid = true;
				required = input[0];
			} else {
				System.out.println("Command does not exist.  Valid commands are:");
				for (Command command : Command.values()) {
					System.out.println(command.getUsage());
				}
			}
		}
	}
	
	@Override
	public Command getCommand() {
		return command;
	}

	@Override
	public boolean isValid() {
		return valid;
	}

	@Override
	public String getRequired() {
		return required;
	}

	@Override
	public List<String> getOptional() {
		return optional;
	}

}
